﻿Ext.define('MyDesktop.Settings', {
    extend: 'Ext.ux.desktop.Module',
	requires: [
		'Ext.data.*',
        'Ext.tab.*',
		'Ext.view.*',
		'Ext.menu.*',
		'Ext.form.*',
		'Ext.tree.*',
		'Ext.ux.DataView.Animated',
		'Ext.ux.TabScrollerMenu',
		'Ext.ux.TabCloseMenu'
    ],
	edit:false,
    id:'settings-win',
	init : function(){
        this.launcher = {
            text: '系统设置',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        };
    },
	userprivileges:function(){
		var me = this;
		return Ext.decode(Ext.getStore('userinfo_store').getRange()[0].get('privileges'));
	},
	createWindow : function(){
		var me = this;
		var desktop = this.app.getDesktop();
        var win = desktop.getWindow('settings-win');
		Ext.override(Ext.form.FieldSet, {
			setExpanded: function(expanded) {
				var me = this,
					checkboxCmp = me.checkboxCmp,
					toggleCmp = me.toggleCmp,
					event;
				expanded = !!expanded;
				if (checkboxCmp) {
					checkboxCmp.setValue(expanded);
				}	
				if (toggleCmp) {
					toggleCmp.setType(expanded ? 'up' : 'down');
				}	
				if (expanded) {
					event = "expand";
					me.removeCls(me.baseCls + '-collapsed');
				} else {
					event = "collapse";
					me.addCls(me.baseCls + '-collapsed');
				}
				me.collapsed = !expanded;
				me.doComponentLayout();
				me.fireEvent(event, me);
				return me;
			}
		});
		if (!Ext.ModelManager.isRegistered('system_settings_user_list_model')){
			Ext.define('system_settings_user_list_model', {
				extend: 'Ext.data.Model',
				fields: [
					{name: 'id', type: 'int'},
					{name: 'name', type: 'string'},
					{name: 'icon', type: 'string'},
					{name: 'privileges', type: 'string'},
					{name: 'create_time', type: 'string'},
					{name: 'login_time', type: 'string'}
				]
			});
		}
		Ext.create('Ext.data.Store',{
			storeId:'system_settings_user_list_store',
			model: 'system_settings_user_list_model',
			//autoSync: true,
			autoLoad: true,
			buffered: true,
			proxy: {
				type: 'ajax',
				url : '/interface.php',
				reader: {
					type: 'json',
					root: 'system_settings_user_list'
				},
				extraParams:{
					type: 'system_settings_user_list'
				}
			}
		});
		if (!Ext.ModelManager.isRegistered('new_system_settings_user_list_model')){
			Ext.define('new_system_settings_user_list_model', {
				extend: 'Ext.data.Model',
				fields: [
					{name: 'id', type: 'string'},
					{name: 'privileges', type: 'string'}
				]
			});
		}
		if (!Ext.getStore('new_system_settings_user_list_store')){
			Ext.create('Ext.data.Store',{
				storeId:'new_system_settings_user_list_store',
				model: 'new_system_settings_user_list_model',
				buffered: true,
				proxy: {
					type: 'memory',
					reader: {
						type: 'json'
					}
				}
			});
		}
		if (!win){
			win = desktop.createWindow({
                id: 'settings-win',
                title:'系统设置',
                width:740,
                height:480,
                iconCls: 'icon-grid',
                animCollapse:false,
                constrainHeader:true,
                layout: 'fit',
				items:[{
					xtype:'tabpanel',
					id:'system_settings',
					enableTabScroll: true,
					tabPosition:'bottom',
					activeTab: 0,
                    dockedItems: [{
                        xtype: 'toolbar',
                        dock: 'top',
                        id:'system_settings_toolsbar',
                        items: [
                            {
                                xtype: 'textfield',
                                name : '搜索',
                                id:'system_settings_search',
                                fieldLabel: '搜索',
                                labelAlign: 'right',
                                labelWidth: 35,
                                listeners: {
                                    scope : this,
                                    buffer: 50,
                                    change: me.filter
                                }
                            },
                            ' ',
                            {
                                xtype: 'combo',
                                id:'system_settings_sort',
                                fieldLabel: '排序',
                                labelAlign: 'right',
                                labelWidth: 45,
                                valueField: 'field',
                                displayField: 'label',
                                value: 'Type',
                                editable: false,
                                store: Ext.create('Ext.data.Store', {
                                    fields: ['field', 'label'],
                                    sorters: 'type',
                                    proxy : {
                                        type: 'memory',
                                        data  : [{label: '用户名', field: 'name'}, {label: '状态', field: 'icon'}]
                                    }
                                }),
                                listeners: {
                                    scope : this,
                                    select: me.sort
                                }
                            }
                        ]
                    }],
					plugins: [{
						ptype: 'tabscrollermenu',
						maxText  : 15,
						pageSize : 5
					},Ext.create('Ext.ux.TabCloseMenu', {
						closeTabText:'关闭页',
						closeOthersTabsText:'关闭其他页',
						closeAllTabsText:'关闭所有页',
						extraItemsTail: [
							'-',
							{
								text: '关闭',
								checked: true,
								hideOnClick: true,
								handler: function (item) {
									currentItem.tab.setClosable(item.checked);
								}
							}
						],
						listeners: {
							aftermenu: function () {
								currentItem = null;
							},
							beforemenu: function (menu, item) {
								var menuitem = menu.child('*[text="关闭"]');
								currentItem = item;
								menuitem.setChecked(item.closable);
							}
						}
					})],
					items: [{
						xtype: 'dataview',
						title:'用户列表',
						id:'system_settings_user_list',
						overItemCls: 'x-view-over',
						tpl: [
							'<tpl for=".">',
								'<div class="settings_user">',
									(!Ext.isIE6? '<img width="64" height="64" src="/images/icons/system/{icon}" />' :'<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="/images/icons/system/{icon}") !important;"></div>'),
									'<h3>{name}</h3>',
								'</div>',
							'</tpl>'
						],
						//plugins: [Ext.create('Ext.ux.DataView.Animated')],
						itemSelector: 'div.settings_user',
						store:'system_settings_user_list_store',
						autoScroll  : true,
						listeners: {
							scope: this,
							//selectionchange: me.onIconSelect,
							itemdblclick: me.onIcondblclick,
							containercontextmenu:me.containerrightclick,
							itemcontextmenu:me.itemrightclick
						}
					}],
                    listeners: {
                        tabchange:function(tabPanel,newCard,oldCard){
                            if (newCard.id != 'system_settings_user_list'){
                                tabPanel.removeDocked(tabPanel.getComponent('system_settings_toolsbar'),false);
                            }else{
                                tabPanel.addDocked(Ext.getCmp('system_settings_toolsbar'));
                            }
                        }
                    }
				}]
			});
		}
		win.show();
        return win;
	},
	onIconSelect:function(dataview, selections){
	},
	onIcondblclick:function(view,record,item,index,e){
		var me = this;
		me.newtab(view,record,item,index,e);
	},
	containerrightclick:function(view,e){
		var me = this;
		if (me.userprivileges().system.admin){
			view.menu = Ext.create('Ext.menu.Menu',me.containerrightclickmenu(view,e));
			view.menu.showAt(e.getXY());
		}
		e.stopEvent();
	},
	itemrightclick:function(view,record,item,index,e){
		var me = this;
		if (me.userprivileges().system.admin){
			view.menu = Ext.create('Ext.menu.Menu',me.itemrightclickmenu(view,record,item,index,e));
			view.menu.showAt(e.getXY());
		}
		e.stopEvent();
	},
	containerrightclickmenu:function(view,e){
		var me = this;
		return {
				defaultAlign: 'br-tr',
				items: [{
					text: '添加用户',
					handler: function(){
						Ext.create('Ext.window.Window',{
							title:'添加用户',
							id:'system_settings_add_user_windows',
							autoShow:true,
							items:[{
								xtype:'form',
								id: 'system_settings_add_user',
							//baseCls: 'x-plain',
								fileUpload: true,
								bodyStyle: 'padding: 10px 10px 0 10px;',
								labelWidth: 80,
								buttonAlign: 'center',
								defaults: {
									anchor: '85%',
									allowBlank: false,
									msgTarget: 'side'
								},
								items:[{
									xtype:'combobox',
									fieldLabel: '用户',
									id:'system_settings_add_user_combobox',
									name: 'id',
									store: new Ext.data.Store({
										autoSync: true,
										autoLoad: true,
										buffered: true,
										proxy: {
											type: 'ajax',
											url : '/interface.php',
											reader: {
												type: 'json',
												root: 'add_user_list'
											},
											extraParams:{
												type: 'add_user_list'
											}
										},
										fields: [
											{name: 'id'},
											{name: 'name'}
										]
									}),
									displayField: 'name',
									//hiddenName:'id',
									valueField: 'id',
									triggerAction: 'all',
									forceSelection: true,
									editable: false,
									allowBlank: false,
									mode: 'local',
									emptyText: '请选择用户'
								}],
								buttons: [{
									text: '增加',
									handler: function(){
										Ext.Msg.confirm('添加用户', '确定添加'+Ext.getCmp('system_settings_add_user_combobox').getRawValue()+'？',function(btn){
											if (btn == 'yes'){
												if(Ext.getCmp('system_settings_add_user').getForm().isValid()){
													Ext.getCmp('system_settings_add_user').getForm().submit({
														url: '/interface.php',
														method: 'POST',
														params: { 
															type: 'add_user',
															name:Ext.getCmp('system_settings_add_user_combobox').getRawValue()
														},
														success: function(form, action) {
															Ext.getCmp('system_settings_add_user_windows').close();
															Ext.Msg.alert('成功', '添加用户成功');
															Ext.getStore('system_settings_user_list_store').load();
															view.update();
														},
														failure: function(form, action) {
															Ext.getCmp('system_settings_add_user_windows').close();
															Ext.Msg.alert('失败', '添加用户失败');
															Ext.getStore('system_settings_user_list_store').load();
															view.update();
														}
													});
												}
											}			
										});
									}
								}]
							}]
						});
					},
					scope: me
				},{
					defaultAlign: 'br-tr',
					text: '刷新',
					handler: function(){
						Ext.getStore('system_settings_user_list_store').load();
					},
					scope: me
				}]
			};
	},
	itemrightclickmenu:function(view,record,item,index,e){
		var me =this;
		if (me.userprivileges().system.login){
		if (Ext.JSON.decode(Ext.getStore('system_settings_user_list_store').findRecord('id',record.data.id).data.privileges).system.login == 1){
			return {
				defaultAlign: 'br-tr',
				items: [{
					text: '禁用',
					handler: function(){
						Ext.Msg.confirm('禁用', '确定禁用'+record.data.name+'？',function(btn){
							if (btn == 'yes'){
								Ext.Ajax.request({
									url: '/interface.php',
									params: {
										type: 'disable_user',
										id:record.data.id
									},
									success: function(response){
										if (response.responseText == 'success'){
											Ext.Msg.alert('状态', '禁用'+record.data.name+'成功！！');
											Ext.getStore('system_settings_user_list_store').load();
											view.update();
										}else{
											Ext.Msg.alert('状态', '禁用'+record.data.name+'失败！！');
											Ext.getStore('system_settings_user_list_store').load();
											view.update();
										}
									}
								});
							}
						});
						
					},
					scope: me
				},{
					text: '删除',
					handler: function(){
						Ext.Msg.confirm('删除', '确定删除'+record.data.name+'？',function(btn){
							if (btn == 'yes'){
								Ext.Ajax.request({
									url: '/interface.php',
									params: {
										type: 'delete_user',
										id:record.data.id
									},
									success: function(response){
										if (response.responseText == 'success'){
											Ext.Msg.alert('状态', '删除'+record.data.name+'成功！！');
											Ext.getStore('system_settings_user_list_store').load();
											view.update();
										}else{
											Ext.Msg.alert('状态', '删除'+record.data.name+'失败！！');
											Ext.getStore('system_settings_user_list_store').load();
											view.update();
										}
									}
								});
							}
						});
						
					},
					scope: me
				}] 
			};
		}else{
			return{
				defaultAlign: 'br-tr',
				items:[{
					text: '启用', 
					handler: function(){
						Ext.Msg.confirm('启用', '确定启用'+record.data.name+'？',function(btn){
							if (btn == 'yes'){
								Ext.Ajax.request({
									url: '/interface.php',
									params: {
										type: 'enable_user',
										id:record.data.id
									},
									success: function(response){
										if (response.responseText == 'success'){
											Ext.Msg.alert('状态', '启用'+record.data.name+'成功！！');
											Ext.getStore('system_settings_user_list_store').load();
											view.update();
										}else{
											Ext.Msg.alert('状态', '启用'+record.data.name+'失败！！');
											Ext.getStore('system_settings_user_list_store').load();
											view.update();
										}
									}
								});
							}
						});
				},
				scope: me
			},{
					text: '删除',
					handler: function(){
						Ext.Msg.confirm('删除', '确定删除'+record.data.name+'？',function(btn){
							if (btn == 'yes'){
								Ext.Ajax.request({
									url: '/interface.php',
									params: {
										type: 'delete_user',
										id:record.data.id
									},
									success: function(response){
										if (response.responseText == 'success'){
											Ext.Msg.alert('状态', '删除'+record.data.name+'成功！！');
											Ext.getStore('system_settings_user_list_store').load();
											view.update();
										}else{
											Ext.Msg.alert('状态', '删除'+record.data.name+'失败！！');
											Ext.getStore('system_settings_user_list_store').load();
											view.update();
										}
									}
								});
							}
						});
						
					},
					scope: me
				}] 
			};
		}
		}
	},
	filter: function(field, newValue) {
        var store = Ext.getStore('system_settings_user_list_store'),
            dataview = Ext.getCmp('system_settings_user_list');  
        store.suspendEvents();
        store.clearFilter();
        dataview.getSelectionModel().clearSelections();
        store.resumeEvents();
        store.filter({
            property: 'name',
            anyMatch: true,
            value   : newValue
        });
    },
    sort: function() {
        var field = Ext.getCmp('system_settings_sort').getValue();
        Ext.getStore('system_settings_user_list_store').sort(field);
    },
	newtab:function(view,record,item,index,e){
        var me = this;
        if (!Ext.getCmp(record.data.id+'_tab')){
            Ext.getCmp('system_settings').add({
                title:record.data.name,
                id:record.data.id+'_tab',
                tabTip: record.data.name,
                closable: true,
                closeAction:'destroy',
                layout:'border',
                items:[{
                    xtype: 'panel',
                    region: 'west',
                    layout:'fit',
                    id:record.data.id+'_info',
                    deferredRender: false,
                    animCollapse:true,
                    items: [{
                        xtype: 'panel',
                        width: '150px',
                        html: '<div id="userinfo"><div id="userinfo-logo"><img src="./images/icons/system/user-tab/'+record.data.icon+'" height="128" width="128"/></div><div id="userinfo-text">'+record.data.name+'</div><div id="userinfo-create-png"></div><div id="userinfo-create">'+record.data.create_time+'</div><div id="userinfo-last-png"><div id="userinfo-last">'+record.data.login_time+'</div></div>',
                        title: '用户信息'
                    }]
                },{
                    xtype: 'panel',
                    region: 'center',
                    title: '用户相关设置',
                    id:record.data.id+'_settings',
                    autoScroll: true,
                    frame:true,
                    items: [],
                    fbar: [{
                        xtype: 'button',
						id:record.data.id+'_save',
                        text: '保存',
                        handler: function() {
							me.saveuserpri(record.data.id,record.data.name);
                        }
                    },{
                        xtype: 'button',
                        text: '取消',
                        handler: function() {
                            Ext.getCmp(record.data.id+'_tab').close();
                        }
                    }]
                }],
                listeners:{
                    destroy:function(){
                        Ext.data.StoreManager.unregister(record.data.id+'_frgbak_landp_tree_store');
                        Ext.data.StoreManager.unregister(record.data.id+'_orig_pri_store');
                        Ext.ModelManager.unregister(record.data.id+'_orig_pri_model');
						Ext.getCmp(record.data.id+'_frgbak_landp_window').hide();
                    }
                }
            });
			me.user_pri(record);
			me.frgbak_pri(record);
        }
        Ext.getCmp(record.data.id+'_tab').show();
	},
	saveuserpri:function(id,name){
		var me = this;
		if (me.edit){
			Ext.MessageBox.show({
				id:'frgbak_saveuserpri',
				title: '确定更改吗？',
				msg: '确定更改'+name+'的权限吗？',
				width:300,
				buttons: Ext.MessageBox.OKCANCEL,
				icon:Ext.MessageBox.WARNING,
				animateTarget: Ext.getCmp(id+'_save'),
				fn:function(btn){
                    if(btn=='ok'){
						Ext.Ajax.request({
							url: '/interface.php',
							params: {
								type:'save_user_pri',
								id:id,
								pri:Ext.getStore('new_system_settings_user_list_store').findRecord('id',id).get('privileges')
							},
							success: function(response){
								if(response.responseText == 'success'){
									Ext.Msg.alert('成功', '更改'+name+'成功！');
								}
							}
						});
					}
				}
			});
		}
		
	},
    getuserpri:function(id,index,fn,args){
        var me = this;
        Ext.Ajax.request({
            url: '/interface.php',
            params: {
                type:'get_user_pri',
                id:id,
                index:index
            },
            success: function(response){
                var tmp = new Array();
                var tmp2 = Ext.decode(response.responseText);
                
                tmp.push({'index':tmp2.getuserpri.index,'pri':tmp2.getuserpri.pri});
                Ext.getStore(id+'_orig_pri_store').add(tmp);
                if (fn){
                    Ext.callback(fn,me,args);
                }
            }
        });
    },
	updateuserpri:function(id,user_pri){
		if(Ext.getStore('new_system_settings_user_list_store').findRecord('id',id)){
			Ext.getStore('new_system_settings_user_list_store').findRecord('id',id).set('privileges',Ext.JSON.encode(user_pri));
		}else{
			Ext.getStore('new_system_settings_user_list_store').add({id:id,privileges:Ext.JSON.encode(user_pri)});
		}
	},
    user_pri:function(record){
        var me = this;
        if (me.userprivileges().system.admin){
            Ext.getCmp(record.data.id+'_settings').insert(0,{
                xtype:'fieldset',
                id:record.data.id+'_user_pri',
                title: '用户系统设置',
                //collapsed: false,
                layout: 'hbox',
                bodyPadding:10,
				width:400,
                items :[{
					xtype:'checkbox',
					margin:'0 15 0 7',
					fieldLabel: '是否可以登录',
					labelWidth:120,
					id: record.data.id+'_user_pri_login',
					name: record.data.id+'_user_pri_login',
					items:[{
						name      : record.data.id+'_user_pri_login_checkbox',
						inputValue: true,
						id        : 'checkbox1'
					}],
					listeners:{
						change:function(field,newvalue,oldvalue){
							if(Ext.getStore('new_system_settings_user_list_store').findRecord('id',record.data.id)){
								var user_pri = Ext.JSON.decode(Ext.getStore('new_system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
							}else{
								var user_pri = Ext.JSON.decode(Ext.getStore('system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
							}
							if (newvalue != user_pri.system.login){
								user_pri.system.login = newvalue;
								me.updateuserpri(record.data.id,user_pri);
								me.edit = true;
							}
						}
					}
				},{
					xtype:'checkbox',
					fieldLabel: '系统管理员',
					labelWidth:120,
					id: record.data.id+'_user_pri_admin',
					name: record.data.id+'_user_pri_admin',
					items:[{
						name      : record.data.id+'_user_pri_admin_checkbox',
						inputValue: true,
						id        : 'checkbox1'
					}],
					listeners:{
						change:function(field,newvalue,oldvalue){
							if(Ext.getStore('new_system_settings_user_list_store').findRecord('id',record.data.id)){
								var user_pri = Ext.JSON.decode(Ext.getStore('new_system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
							}else{
								var user_pri = Ext.JSON.decode(Ext.getStore('system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
							}
							if (newvalue != user_pri.system.admin){
								user_pri.system.admin = newvalue;
								me.updateuserpri(record.data.id,user_pri);
								me.edit = true;
							}
						}
					}
                }]
            });
			var user_pri = Ext.JSON.decode(Ext.getStore('system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
            if (user_pri.system.login){
                Ext.getCmp(record.data.id+'_user_pri_login').setValue(true);
            }
            if (user_pri.system.admin){
                Ext.getCmp(record.data.id+'_user_pri_admin').setValue(true);
            }
        }
    },
    frgbak_pri:function(record){
        var me = this;
		if ((me.userprivileges().system.admin)||(me.userprivileges().frgbak.admin)){
            if (!Ext.getStore(record.data.id+'_frgbak_landp_tree_store')){
                Ext.create('Ext.data.TreeStore', {
                    storeId:record.data.id+'_frgbak_landp_tree_store',
                    proxy: {
                        type: 'ajax',
                        url: '/interface.php?type=frgbak_landp_tree&id='+record.data.id,
                        method: 'POST',
                        reader: 'json'
                    },
                    root: {
                        text: '富人国',
                        id: 'root',
                        expanded: true
                    },
                    folderSort: true,
                    sorters: [{
                        property: 'text',
                        direction: 'ASC'
                    }]
                });
            }
            if(!Ext.getCmp(record.data.id+'_frgbak')){
                if(me.userprivileges().system.admin){
					Ext.getCmp(record.data.id+'_settings').add({
						xtype:'fieldset',
						id:record.data.id+'_frgbak',
						checkboxToggle:true,
						title: '富人国备份',
						collapsed: true,
						defaults: {anchor: '100%'},
						layout: 'anchor',
						bodyPadding:10,
						width:400,
						items :[]
					});
				}else if(me.userprivileges().frgbak.admin){
					Ext.getCmp(record.data.id+'_settings').add({
						xtype:'fieldset',
						id:record.data.id+'_frgbak',
						title: '富人国备份',
						collapsed: true,
						defaults: {anchor: '100%'},
						layout: 'anchor',
						bodyPadding:10,
						width:400,
						items :[]
					});
				}
				Ext.getCmp(record.data.id+'_frgbak').add({
					frame: true,
					layout: 'hbox',
					items:[{
						xtype:'checkbox',
						margin:'3 10 0 5',
						fieldLabel: '富人国备份管理员',
						labelWidth:120,
						id: record.data.id+'_user_frgbak_admin',
						inputValue: true
					},{
						xtype:'label',
						margin:'5 0 0 5',
						width:120,
						text: '可备份的合作方:'
					},{
						xtype:'button',
						id:record.data.id+'_frgbak_landp_button',
						text   : null,
						iconCls: 'add',
						scale: 'large',
						autoWidth:true,
						handler: function() {
							function Tree_Node_Children_Select(pNode, checked,id,me){
								pNode.set('checked', checked);
								if(Ext.getStore('new_system_settings_user_list_store').findRecord('id',id)){
									var user_pri = Ext.JSON.decode(Ext.getStore('new_system_settings_user_list_store').findRecord('id',id).get('privileges'));
								}else{
									var user_pri = Ext.JSON.decode(Ext.getStore('system_settings_user_list_store').findRecord('id',id).get('privileges'));
								}
								if (checked){
									if(pNode.isLeaf()){
										if(Ext.Array.indexOf(user_pri.frgbak.partner,pNode.data.id) == -1){
											user_pri.frgbak.partner.push(pNode.data.id);
										}
										if(Ext.Array.indexOf(user_pri.frgbak.lang,pNode.data.parentId) == -1){
											user_pri.frgbak.lang.push(pNode.data.parentId);
										}
										me.updateuserpri(id,user_pri);
										me.edit = true;
									}
								}else{
									if(pNode.isLeaf()){
										Ext.Array.remove(user_pri.frgbak.partner,pNode.data.id);
										me.updateuserpri(id,user_pri);
										me.edit = true;
									}else{
										Ext.Array.remove(user_pri.frgbak.lang,pNode.data.id);
										me.updateuserpri(id,user_pri);
										me.edit = true;
									}
								}
								Ext.each(pNode.childNodes, function(childNode){
									Tree_Node_Children_Select(childNode, checked,id,me);
								});
							}
							if(!Ext.getCmp(record.data.id+'_frgbak_landp_window')){
								Ext.create('Ext.window.Window',{
									title:'可备份的合作方',
									id:record.data.id+'_frgbak_landp_window',
									//autoDestroy:true,
									closeAction:'hide',
									width:100,
									animateTarget:Ext.getCmp(record.data.id+'_frgbak_landp_button'),
									items:[{
										xtype:'treepanel',
										id:record.data.id+'_frgbak_landp_tree',
										height:200,
										autoScroll:true,
										store:record.data.id+'_frgbak_landp_tree_store',
										useArrows: true,
										animate:true,
										loadMask: true,
										forceFit:true,
										frame: false,
										rootVisible: false,
										listeners: {  
											itemclick: function(view, record, item, index, e){  
											}, 
											checkchange: function(node, checked){
												node.expand(true,function(){                                            
													Tree_Node_Children_Select(node, checked, record.data.id, me);
												});
											}  
										}
									}]
								});
							}
							Ext.getCmp(record.data.id+'_frgbak_landp_window').show();
						}
					}]
				});
            }
			Ext.Function.defer(function(){
				var frgbak_pri = Ext.JSON.decode(Ext.getStore('system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
				if (frgbak_pri.system.index){
					Ext.each(frgbak_pri.system.index,function(node){
						if (node == 'frgbak'){
							Ext.getCmp(record.data.id+'_frgbak').expand();
							if (frgbak_pri.frgbak.admin){
								Ext.getCmp(record.data.id+'_user_frgbak_admin').setValue(true);
							}
						}
					});
				}
				if(!me.userprivileges().system.admin){
					Ext.getCmp(record.data.id+'_user_frgbak_admin').setDisabled(true);
				}
				Ext.getCmp(record.data.id+'_frgbak').on('collapse',function(){
					if(Ext.getStore('new_system_settings_user_list_store').findRecord('id',record.data.id)){
						var user_pri = Ext.JSON.decode(Ext.getStore('new_system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
					}else{
						var user_pri = Ext.JSON.decode(Ext.getStore('system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
					}
					Ext.Array.remove(user_pri.system.index,'frgbak');
					var newfrgbak = {"system":user_pri.system};
					me.updateuserpri(record.data.id,newfrgbak);
					me.edit = true;
				});
				Ext.getCmp(record.data.id+'_frgbak').on('expand',function(){
					if(Ext.getStore('new_system_settings_user_list_store').findRecord('id',record.data.id)){
						var user_pri = Ext.JSON.decode(Ext.getStore('new_system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
					}else{
						var user_pri = Ext.JSON.decode(Ext.getStore('system_settings_user_list_store').findRecord('id',record.data.id).get('privileges'));
					}
					if(Ext.Array.indexOf(user_pri.system.index,'frgbak') == -1){
						user_pri.system.index.push('frgbak');
					}
					var newfrgbak = {"system":user_pri.system,"frgbak":{"admin":false,"lang":[],"partner":[]}};
					me.updateuserpri(record.data.id,newfrgbak);
					me.edit = true;
				});
            }, 100);
        }
    }
});